package com.ljz.common.dao;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljz.common.dao.bean.PageParam;
import com.ljz.common.dao.bean.PageResult;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;

@SuppressWarnings("all")
public class MyBaseServiceImpl<T,M extends MyBaseMapper> implements MyBaseService<T>{

    @Autowired
    protected M baseMapper;

    @Override
    public int saveEntity(T entity) {
        return baseMapper.insert(entity);
    }

    @Override
    public int updateEntity(T entity) {
        return baseMapper.updateById(entity);
    }

    @Override
    public int deleteEntity(Serializable id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public T queryEntity(Serializable id) {
        return (T) baseMapper.selectById(id);
    }

    @Override
    public <P extends PageParam> PageResult<T> queryByPage(P params) {

        PageHelper.startPage(params.getPage(), params.getPageSize());
        List<T> list = baseMapper.queryByPage(params);
        PageInfo<T> pageInfo = new PageInfo(list);
        return new PageResult<>(params.getPage(),params.getPageSize(),pageInfo.getTotal(),list);
    }
}
